exports.GAIsometricView = Core.MessageListener.subclass({
	classname : 'GAIsometricView',

	initialize: function(createPatches) {
		this._createPatches = typeof createPatches == 'undefined' ? true : createPatches;  
		this._numTouches = 0;
		this._countExpanded = 0;
		var w = Core.Capabilities.getScreenWidth();
		var h = Core.Capabilities.getScreenHeight();
		this._multiArray = [];
		this._diffForPinch = 0;
		this._lastDistance = 0;
		this._isPanningDisabled = false;
		this._isPinchingDisabled = false;
		this._backgroundPatch = [];
		var maxRows = GAIsoMetricConfigurations.getMaxGridRows();
		this._gridObjects = new Array(maxRows);
		for( var i = maxRows; i--;) {
			this._gridObjects[i] = new Array(maxRows);
		}

		var gridSizeX = GAIsoMetricConfigurations.getGridSize().getX();
		var gridSizeY = GAIsoMetricConfigurations.getGridSize().getY();

		var width = gridSizeX*GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH*2 + GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_WIDTH*2;
		var height = gridSizeY*GAIsoMetricConfigurations.USERLAYER_CELL_HEIGHT*2 + GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_HEIGHT*2;

		this._width = width;
		this._height = height;

		this._target = new GL2.TouchTarget();
		this._target.setAnchor(0.5, 0.5);
		this._target.setSize(width,height);
		this._target.getTouchEmitter().addListener(this, this.onTouch);
		this._target.setDepth(100);
		
		var motherSprite = new GL2.Sprite();
		motherSprite.setImage(GAIsoMetricConfigurations.getBgPath(), [width, height], [.5, .5]);
		motherSprite.setPosition([h/2,w/2]);
		motherSprite.addChild(this._target);
		this._motherSprite = motherSprite;
		//Patch Image changes
		this._createPatches = false;
		////////
		if (this._createPatches) {
			var gSize = GAIsoMetricConfigurations.getGridSize();
			for(var j = 0; j < gSize.getY() ; j = j + 4) {
				for(var i = 0; i < gSize.getX(); i = i + 4) {
					var patchSprite = new GL2.Sprite();
					patchSprite.setDepth(2);
					patchSprite.setAlpha(GAIsoMetricConfigurations.getPatchTransparency());
					patchSprite.setImage(GAIsoMetricConfigurations.getPatchPath(), [GAIsoMetricConfigurations.getPatchWidth(), GAIsoMetricConfigurations.getPatchHeight()], [0.5, 1]);
					var tempVector = new Core.Vector(i,j);
	
					tempVector = this.convertGridPointToLayerPoint(tempVector);
	
					patchSprite.setPosition(tempVector);
					this._backgroundPatch.push(patchSprite);
					this._motherSprite.addChild(patchSprite);			                
				}
			}
		}
		else{
			this.initializeBoundry();
		}
	},
	initializeBoundry:function(){
		
		var i , j , patchSprite = null , tempVector = null;
		
		if(!this._gridPatches)
		{
			this._gridPatches = new Array();
		}
		
		
		patchSprite = new GL2.Sprite();
		patchSprite.setDepth(2);
		
		var gridSize = GAIsoMetricConfigurations.getGridSize();//GameState.getCurrentUser().getRanch().getRanchGridSize();
		
		
		NgLogE("initializeBoundry::gridSize: " + gridSize.getX()/4);
		var dimensions = TRUtil.getExpansionDimensions(gridSize.getX()/4);
		patchSprite.setImage('./Content/images/backgroundSlices/'+ gridSize.getX()/4 +'x' + gridSize.getX()/4+'-grid.png', [dimensions.w, dimensions.h], [0.5, 0.5]);
		tempVector = new Core.Vector(dimensions.x,dimensions.y);
		
		tempVector = this.convertGridPointToLayerPoint(tempVector);
		patchSprite.setPosition(tempVector);
		this._motherSprite.addChild(patchSprite);
		this._gridPatches.push(patchSprite);
			
		
	},
	removeAllGridPatches: function(){
		if(this._gridPatches)
		{
			var count = this._gridPatches.length, i=0;
			for(; i < count ; i++)
			{
				var patchSprite = this._gridPatches.pop();
				patchSprite.destroy();
				patchSprite = null;
			}
			delete this._gridPatches;
			this._gridPatches = null;
		}
	},
														
	
	getView: function() {
		return this._motherSprite;
	},

	addChild: function(node, zOrder){
		node.setDepth(zOrder);
		this._motherSprite.addChild(node);
	},

	expandView : function (increaseBy){
														
		/*this._loadingScreen = new GameLoadingScreen();
		this._loadingScreen.startLoadingScreen();
		this._loadingScreen.updateProgress(0);
		var myString = "Expandion In Progress.."//GALocalizationHandler.getRelativeString("Downloading Images: %val1 %", ["0"]);
		this._loadingScreen.showProgressLabel(myString);*/
														
		var gSize = GAIsoMetricConfigurations.getGridSize();
		var factor = increaseBy;
		increaseBy.setX((4*increaseBy.getX()) + gSize.getX());
		increaseBy.setY((4*increaseBy.getY()) + gSize.getY());
		if(increaseBy.getX() < GAIsoMetricConfigurations.getMaxGridRows()){		    		

			this._diffForPinch = 0;
			this._lastDistance = 0;
			this._isPanningDisabled = false;
			this._isPinchingDisabled = false;

			/*var child;
			while(child = this._backgroundPatch.pop()){
				//this._motherSprite.removeChild(child);
				child.destroy();
			}*/
			this.removeAllGridPatches()
			
			var gridSizeX = increaseBy.getX();
			var gridSizeY = increaseBy.getY();

			var width = gridSizeX*GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH*2 + GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_WIDTH*2;
			var height = (gridSizeY * GAIsoMetricConfigurations.USERLAYER_CELL_HEIGHT * 2) + (GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_HEIGHT*2);

			this._width = width;
			this._height = height;
			var h = Core.Capabilities.getScreenHeight();
			var w = Core.Capabilities.getScreenWidth();

			this._motherSprite.setImage(GAIsoMetricConfigurations.getBgPath(), [width, height], [.5, .5]);
			this._ratio = +(gSize.getX()%60)/8;
			//this._motherSprite.setPosition(w - this._width/(7+this._ratio), h - this._height/(7+this._ratio));
			this._motherSprite.setPosition([h/2,w/2]);

			
			this._target.setSize(width,height);
														
			var ZoomOutFactor = 0.45 - (0.018 * (gSize.getX()/4 - 15));
			this.setZoom(ZoomOutFactor);

			GAIsoMetricConfigurations.setGridSize(increaseBy);

			//for(var i = 0; i < increaseBy.getX(); i = i + 4) {
//				for(var j = 0; j < increaseBy.getY() ; j = j + 4) {
//					var patchSprite = new GL2.Sprite();
//					patchSprite.setDepth(2);
//					patchSprite.setAlpha(GAIsoMetricConfigurations.getPatchTransparency());
//					patchSprite.setImage(GAIsoMetricConfigurations.getPatchPath(), [GAIsoMetricConfigurations.getPatchWidth(), GAIsoMetricConfigurations.getPatchHeight()], [0.5, 1]);
//					var tempVector = new Core.Vector(i,j);
//
//					tempVector = this.convertGridPointToLayerPoint(tempVector);
//
//					patchSprite.setPosition(tempVector);
//					this._backgroundPatch.push(patchSprite);
//					this._motherSprite.addChild(patchSprite);
//				}
//			}
		    this.initializeBoundry();
			NgLogE("Sunilla:: after initializing boundry")
			for (var i = gSize.getX() ; i-- ;){
				for (var j = gSize.getY() ; j-- ; ){
					if(this._gridObjects[i][j] != null && this._gridObjects[i][j] != 'undefined'){
						var gL = this._gridObjects[i][j].getGridLocation();
						var tempVector = new Core.Vector(gL.getX(),gL.getY());
						tempVector = this.convertGridPointToLayerPoint(tempVector);
						this._gridObjects[i][j].setObjectPosition(tempVector);
						this._gridObjects[i][j].getObject().setDepth(GameController.getTRUserLayer().getZ(this._gridObjects[i][j],gL));
					}
				}
			}
		
		}
		//setTimeout(this._loadingScreen.endLoadingScreen(),2000);

	},
	
	removeChild: function(node){
		this._motherSprite.removeChild(node);
	},

	getMotherSprite: function(){
		return this._motherSprite;
	},
	
	convertGridPointToLayerPoint: function(posVector) {

		var newVector = new Core.Vector();
		newVector.setX(posVector.getX() *(GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH + GAIsoMetricConfigurations.getCellWidthOffset()));
		newVector.setY(posVector.getY() *(GAIsoMetricConfigurations.USERLAYER_CELL_HEIGHT + GAIsoMetricConfigurations.getCellHeightOffset()));

		var xShear = -0.73;
		newVector.setX(newVector.getX() + (newVector.getY()*xShear));

		var x = newVector.getX() * 0.894427191000276 - newVector.getY() * 0.447213595499237;
		var y = newVector.getX() * 0.447213595499237 + newVector.getY() * 0.894427191000276;	

		var gridSize = GAIsoMetricConfigurations.getGridSize();

		x = x + gridSize.getX()*GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH;
		x = x + GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_WIDTH;
		y = y + GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_HEIGHT;

		x = x - this._width/2;
		y = this._height - y - (this._height/2);

		var newVector = new Core.Vector(x,y);
//		console.log("x,y = " + x + ", " + y );
		return newVector;
	},

	convertLayerPointToGridPoint: function(point) {

		var gridSize = GAIsoMetricConfigurations.getGridSize();

		point.setX(point.getX() - gridSize.getX()*GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH);

		point.setX(point.getX() - GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_WIDTH);
		point.setY(point.getY() - GAIsoMetricConfigurations.DIAMOND_LAYER_MARGIN_HEIGHT);

		var x = point.getX() * 0.894427191000276 - point.getY() * (-0.447213595499237);
		var y = point.getX() * (-0.447213595499237) + point.getY() * 0.894427191000276;	

		var xShear = 0.73;
		x = x + y*xShear;

		x = x / (GAIsoMetricConfigurations.USERLAYER_CELL_WIDTH + GAIsoMetricConfigurations.getCellWidthOffset());
		y = y / (GAIsoMetricConfigurations.USERLAYER_CELL_HEIGHT + GAIsoMetricConfigurations.getCellHeightOffset());

		var resultVector = new Core.Vector(Math.floor(x),Math.floor(y));
		return resultVector;
	},

	olTouchBegan: function(touch)
	{

	},

	touchBegan: function(touch)
	{
		this.olTouchBegan(touch);
		
		this._multiArray[this._numTouches++] = touch;

		if(this._numTouches == 1)// save this one for offset
		{
			var panningOffset = new Core.Vector();
			panningOffset.setX(this._motherSprite.getPosition().getX() - this._multiArray[0].getPosition().getX());
			panningOffset.setY(this._motherSprite.getPosition().getY() - this._multiArray[0].getPosition().getY());
			this._panningOffset = panningOffset;
														
			this._isTouchMove = false;
		}
	},

	olTouchMoved: function(touch)
	{

	},

	touchMoved: function(touch)
	{
		this.olTouchMoved(touch, this._numTouches);
		if (this._isPanningDisabled){
			return;
		}
		for(var i = this._numTouches-1; i >= 0; i--) {
			var touch1 = this._multiArray[i];
			if(touch1.getId() == touch.getId()) {
				this._multiArray[i] = touch;
			}
		}

		this.multiTouch();
	},

	olTouchEnded: function(touch)
	{

	},

	touchEnded: function(touch)
	{
		this.olTouchEnded(touch);
		for(var i = this._numTouches-1; i >= 0; i--) {
			if(this._multiArray[i].getId() == touch.getId()) {
				this._multiArray.splice(i, 1);
				this._numTouches--;
			}
		}

		if(this._numTouches == 0)//reset everything
		{
			this._diffForPinch = 0;
			this._lastDistance = 0;
		}
		else if (this._numTouches == 1){
			this._panningOffset.setX(this._motherSprite.getPosition().getX() - this._multiArray[0].getPosition().getX());
			this._panningOffset.setY(this._motherSprite.getPosition().getY() - this._multiArray[0].getPosition().getY());
		}
		//this.conversionToLayer(touch);
	},

	onTouch: function(touch)
	{
		switch(touch.getAction())
		{
		case touch.Action.Start:
			this.touchBegan(touch);
			return true;
		case touch.Action.End:
			this.touchEnded(touch);
			break;
		case touch.Action.Move:
			this.touchMoved(touch);
			break;
		}
	},

	conversionToLayer: function(touch)
	{
		var posVector = new Core.Vector(touch.getPosition().getX(),touch.getPosition().getY());
		posVector = this._motherSprite.screenToLocal(posVector);
		posVector = this.adjustAnchorPoint(posVector);
		posVector = this.convertLayerPointToGridPoint(posVector);
		return posVector;
	},
	adjustAnchorPoint: function(posVector) {
		return new Core.Vector(posVector.getX()+this._width/2, -posVector.getY()+this._height/2);
	},

	multiTouch: function()
	{   
		if(this._numTouches == 2) // it's a pinch
		{
			var touch1 = this._multiArray[0];
			var touch2 = this._multiArray[1];

			var distance = 0;

			var location1 = touch1.getPosition();
			var location2 = touch2.getPosition();

			distance = Math.sqrt( Math.pow(location1.getX()-location2.getX(),2) +  Math.pow(location1.getY()-location2.getY(),2));

			var diff = this._lastDistance - distance;

			this._lastDistance = distance;

			this._diffForPinch += diff;
			if(Math.abs(this._diffForPinch) >2) {
				var currentScale = this._motherSprite.getScale();
				var scaleFactor = .05; // why hard coded value??
				if(this._diffForPinch <0) {

					var zoomIn = GAIsoMetricConfigurations.getZoomInFactor();

					if(currentScale.getX() <= zoomIn) {
						this._motherSprite.setScale(currentScale.getX() + scaleFactor,currentScale.getY()+scaleFactor);
						this.zoomCb(this._motherSprite.getScale(), ZoomType.ZoomIn);
					}
				}
				else {
					var zoomOut = GAIsoMetricConfigurations.getZoomOutFactor();
					//var h = GACommonUtils.getScreenHeight();
//					var w = GACommonUtils.getScreenWidth();
//					var fact = w > h ? w : h;
//					var div = w > h ? this._width : this._height;
//					var minScale = (fact/div);
//					if(div > fact){
//						zoomOut = minScale;
//					}
					if(currentScale.getX() - scaleFactor >= zoomOut) {
						this._motherSprite.setScale(currentScale.getX() - scaleFactor,currentScale.getY() - scaleFactor);
						this.zoomCb(this._motherSprite.getScale(), ZoomType.ZoomOut);
					}
				}
				this._diffForPinch = 0;

				/////////

				var offsetAdjustedVector = this._motherSprite.getPosition();

				var currScale = this._motherSprite.getScale().getX();
				if(offsetAdjustedVector.getX() > (this._width/2.0)*currScale)
				{
					offsetAdjustedVector.setX((this._width/2.0)*currScale);
					//NgLogE("limit");
				}
				else if(offsetAdjustedVector.getX() + ((this._width/2)*currScale) <  Core.Capabilities.getScreenHeight()){
					offsetAdjustedVector.setX(Core.Capabilities.getScreenHeight() - (this._width/2)*currScale);
				}
				if(offsetAdjustedVector.getY() > (this._height/2.0)*currScale)
				{
					offsetAdjustedVector.setY((this._height/2.0)*currScale);
					//NgLogE("limit");
				}
				else if(offsetAdjustedVector.getY() + ((this._height/2)*currScale) < Core.Capabilities.getScreenWidth()){
					offsetAdjustedVector.setY(Core.Capabilities.getScreenWidth() - (this._height/2)*currScale);
				}
				var sampleVect = new Core.Vector(this._motherSprite.getPosition().getX() + ((this._width/2.0)*currScale),this._motherSprite.getPosition().getY() + ((this._height/2.0)*currScale));

				var result = this._motherSprite.getParent().localToScreen(sampleVect);

				this._motherSprite.setPosition(offsetAdjustedVector);
				//////////

			}
		}
		else if(this._numTouches == 1) // it's panning
		{
			//console.log(">>>>> bhi jeee in move panedd 1 "+this._motherSprite.getPosition().getX()+" "+this._motherSprite.getPosition().getY());
			var offsetAdjustedVector = new Core.Vector();
			offsetAdjustedVector.setX(this._multiArray[0].getPosition().getX() + this._panningOffset.getX());
			offsetAdjustedVector.setY(this._multiArray[0].getPosition().getY() + this._panningOffset.getY());
			//this._motherSprite.setPosition(offsetAdjustedVector);
			//console.log(">>>>> bhi jeee in move panedd 2 "+offsetAdjustedVector.getX()+" "+offsetAdjustedVector.getY());

			var currScale = this._motherSprite.getScale().getX();
			if(offsetAdjustedVector.getX() > (this._width/2.0)*currScale)
			{
				offsetAdjustedVector.setX((this._width/2.0)*currScale);
			}
			else if(offsetAdjustedVector.getX() + ((this._width/2)*currScale) <  Core.Capabilities.getScreenHeight()){
				offsetAdjustedVector.setX(Core.Capabilities.getScreenHeight() - (this._width/2)*currScale);
			}

			if(offsetAdjustedVector.getY() > (this._height/2.0)*currScale)
			{
				offsetAdjustedVector.setY((this._height/2.0)*currScale);
			}
			else if(offsetAdjustedVector.getY() + ((this._height/2)*currScale) < Core.Capabilities.getScreenWidth()){
				offsetAdjustedVector.setY(Core.Capabilities.getScreenWidth() - (this._height/2)*currScale);
			}
			var xDiff = offsetAdjustedVector.getX()	- this._motherSprite.getPosition().getX() , yDiff = offsetAdjustedVector.getY()	- this._motherSprite.getPosition().getY();
			
			if(!this._isTouchMove && (xDiff > -6 && xDiff < 6) && (yDiff > -6 && yDiff < 6))
			{
				//console.log(">>>>> bhi jeee in move panedd 3 "+this._motherSprite.getPosition().getX()+" "+this._motherSprite.getPosition().getY());
				return;
			}
			this._isTouchMove = true;
			this._motherSprite.setPosition(offsetAdjustedVector);
		}
	},
	
	setZoom: function(level) {
		var currentScale = this._motherSprite.getScale();
		var zoomIn = GAIsoMetricConfigurations.getZoomInFactor();
		var zoomOut = GAIsoMetricConfigurations.getZoomOutFactor();
		var h = GACommonUtils.getScreenHeight();
		var w = GACommonUtils.getScreenWidth();
		var fact = w > h ? w : h;
		var div = w > h ? this._width : this._height;
		var minScale = (fact/div);
		if(div > fact) {
			zoomOut = minScale;
		}
		if(level < zoomOut) {
			level = zoomOut;
		}
		else if(level >= zoomIn) {
			level = zoomIn;
		}
		this._motherSprite.setScale(level, level);
	},

	zoomCb: function(scale, zoomType) {
		
	},

	destroy: function()
	{
		for(var i = this._numTouches-1; i >= 0; i--) {
			this._multiArray[i] = null;
		}
		this._multiArray = null;

		this._diffForPinch = null;
		this._lastDistance = null;
		this._isPanningDisabled = null;
		this._isPinchingDisabled = null;
		this._target = null;
		this._panningOffset = null;
		this._multiArray = null;
		this.olTouchEnded = null;

		var maxRows = GAIsoMetricConfigurations.getMaxGridRows();
		for( var i = maxRows; i--;) {
			this._gridObjects[i] = null;
		}
		this._gridObjects = null;

		var children = this._motherSprite.getChildren();
		var len = children.length;
		for (var i = 0; i < len; i++) {
			if(Core.ObjectRegistry.isObjectRegistered(children[i])){
			   children[i].destroy();
			}
			children[i] = null;
		}
		this._motherSprite.destroy();
		this._motherSprite = null;
		
		len = this._backgroundPatch.length;
		for (var i = 0; i < len; i++) {
			this._backgroundPatch[i] = null;
		}
		this._backgroundPatch = null;
	}

});